<?php

/**
 * MVM_MALL 网上商店系统 插件管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-14 $
 * $Id: plugins.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if ($action=='list') {
    $list = get_dirinfo(MVMMALL_ROOT.'./plugins/');
    for ($i = 0; $i < count($list); $i++) {
        require MVMMALL_ROOT.'./plugins/'.$list[$i].'/lib/'.$list[$i].'.class.php';
        $hack_name = dhtmlchars(str_replace('.class.php','',$list[$i]));
        $hack_rt  = $db->get_one("SELECT id,hack_name FROM `{$tablepre}plugins` WHERE hack_name='$hack_name'");
        if($hack_rt['hack_name']){
            $hack[$hack_rt['hack_name']]['install'] = 1;
            $hack[$hack_rt['hack_name']]['id']      = $hack_rt['id'];
            $hack[$hack_rt['hack_name']]['edit']    = "admincp.php?module=$module&action=edit&uid=$hack_rt[id]";
            $hack[$hack_rt['hack_name']]['del']     = "admincp.php?module=$module&action=del&uid=$hack_rt[id]";
        }
        $hack[$hack_name]['insert']                      = "admincp.php?module=$module&action=add&hack_name=$hack_name";
    }
    require template('plugins');
    footer();
} elseif ($action=='add' && isset($hack_name)) {
    if($setp==1 && $_POST){
        $hack_rt = $db->get_one("SELECT id,hack_name FROM `{$tablepre}plugins` WHERE hack_name='$hack_name'");
        if($hack_rt['hack_name']){
            exit('该插件已经存在了');
        } else {
            if(!file_exists(MVMMALL_ROOT."./plugins/$hack_name/lib/$hack_name.class.php")){
               exit('该插件不存在');
            } else {
                require_once  MVMMALL_ROOT."./plugins/$hack_name/lib/$hack_name.class.php";
            }
            $config = array();
            foreach ($hack[$hack_name]['cfg'] AS $key => $val)
            {
                $config[$key]['name']   = $val['name'];
                $config[$key]['value']  = $$val['name'];
                $config[$key]['label']  = $val['label'];
            }
            $config = serialize($config);
            $query = "INSERT INTO `{$tablepre}plugins` SET
                  hack_name   = '$hack_name',
                  name        =  '$name',
                  hack_desc   =  '$hack_desc',
                  install     = '1',
                  hack_cfg    = '$config'
                 ";
            $db->query($query);
        }
        admin_msg('success','admincp.php?module=plugins&action=list');
    } else {
        $hack_name   = dhtmlchars($hack_name);
        require_once MVMMALL_ROOT."./plugins/$hack_name/lib/$hack_name.class.php";
        $fields = array();
        foreach ($hack[$hack_name]['cfg'] AS $key => $val)
        {
            $fields[$key]['name']   = $val['name'];
            $fields[$key]['value']  = $val['value'];
            $fields[$key]['label']  = $val['label'];
        }
        @extract($hack[$hack_name],EXTR_OVERWRITE);
        require template('plugins_add');
        footer();
    }
} elseif ($action=='edit' && is_numeric($uid)) {
    if($setp==1 && $_POST){
        $hack_rt    = $db->get_one("SELECT id,hack_name FROM `{$tablepre}plugins` WHERE id='$uid'");
        $hack_name  = $hack_rt['hack_name'];
        $name       = dhtmlchars($name);
        $nums       = $db->counter("{$tablepre}plugins","where  name='$name' AND id <> '$uid'");
        if($nums>0){
            exit('该插件已经存在了');
        } else {
            if(!file_exists(MVMMALL_ROOT."./plugins/$hack_name/lib/$hack_name.class.php")){
               exit('该插件不存在');
            } else {
                require_once  MVMMALL_ROOT."./plugins/$hack_name/lib/$hack_name.class.php";
            }
            $config = array();
            foreach ($hack[$hack_name]['cfg'] AS $key => $val)
            {
                $config[$key]['name']   = $val['name'];
                $config[$key]['value']  = $$val['name'];
                $config[$key]['label']  = $val['label'];
            }
            $config = serialize($config);
            $query = "UPDATE `{$tablepre}plugins` SET
                  hack_name   = '$hack_name',
                  name        =  '$name',
                  hack_desc   =  '$hack_desc',
                  hack_cfg    = '$config'
                  WHERE id    = '$uid' ";
            $db->query($query);
        }
        admin_msg('success',dreferer());
    } else {
        $hack_rt    = $db->get_one("SELECT * FROM `{$tablepre}plugins` WHERE id='$uid'");
        $hack_rt ['hack_cfg'] = unserialize($hack_rt['hack_cfg']);
        $fields     = array();
        foreach ($hack_rt ['hack_cfg'] AS $key => $val)
        {
            $fields[$key]['name']   = $val['name'];
            $fields[$key]['value']  = $val['value'];
            $fields[$key]['label']  = $val['label'];
        }
        $name        = $hack_rt['name'];
        $hack_desc   = $hack_rt['hack_desc'];
        require template('plugins_add');
        footer();
    }
} elseif ($action=='del' && is_numeric($uid)){
    $db->query("DELETE FROM `{$tablepre}plugins` WHERE id='$uid'");
    admin_msg('success','admincp.php?module=plugins&action=list');
} else {
    admin_msg('pass_worng');
}